#! /bin/python
# coding: utf-8

'''
T.Deheurles
04/06/2011
ELI313_IA/062/Exo1
'''
import sys
import random
from PyQt4 import QtGui, QtCore

class IA(object):
    def __init__(self):
        self._observer = None
        self._memoire  = [ 0,-1, 0, 0,
                           1, 0,-1, 0,
                           0, 1, 0,-1,
                           0, 0, 1, 0]
        self.initialise()
        
    def _initGrille(self):
        grille = []
        for i in range(0, 10):
            grille.append(random.randint(0,3))
        return grille

    def _vision(self):
        return self._grille[self._temps]
        
    def _prendsDecision(self):
        case = 4 * self._vision() + self._etat
        reussite = self._memoire[case]
        if case in [0,1,4,5,6,9,10,11,14,15]:
            self._reussite += 1
        return reussite

    def initialise(self):
        self._etat     = random.randint(0,3) #position sur la grille
        self._temps    = 0
        self._grille   = self._initGrille()
        self._reussite = 0
        if self._observer != None:
            self.start()

    def start(self):
        self._observer.addCueilleur(self._etat)
        self._observer.addNourriture(self._grille)

    def actionne(self):
        self._etat += self._prendsDecision()
        if self._etat == self._grille[self._temps]:
            self._observer.updateUi(self._etat, self._temps)
        else:
            self._observer.updateUi(self._etat, None)
        self._temps += 1
        if self._temps == 10:
            self._observer.stop(self._reussite)

    def addObserver(self, observer):
        self._observer = observer

class Ui(QtGui.QWidget):
    def __init__(self, IA):
        super(QtGui.QWidget, self).__init__()
        self._ia = IA
        self._center()
        self.setWindowTitle(u'ELI313_IA - 060 - Exercice n°1')
        self._height = 350
        self._width  = 600
        self.resize(self._width, self._height)
        self._initBouton()
        self._nourritures = []
        self._initFont()
        self._initLabelResultat()
        
    def updateUi(self, position, temps):
        x = self._cueilleur.x()
        self._cueilleur.move(x + 50, 110 + 50 * position)
        if temps != None:
            self._nourritures[temps].close()
        
    def stop(self, nombre):
        self._pauseButton.stop(nombre)
        self._labelResultat.setText(u"Le test est terminé, le résultat est de "\
                     + str(nombre) + " nourritures")
        
    def _reinitialise(self):
        print "pouet"
        self._cueilleur.close()
        for nourriture in self._nourritures:
            nourriture.close()
        self._nourritures = []
        self._cueilleur = None
        
        self._initFont()
        self._ia.initialise()
        self._labelResultat.setText("")
        self.setVisible(True)
        self._cueilleur.setVisible(True)
        for nourriture in self._nourritures:
            nourriture.setVisible(True)
        
    def _initBouton(self):
        self._pauseButton = BoutonPause("Actionne", self,
                                        self._ia.actionne,
                                        self._reinitialise )
        self._pauseButton.setGeometry(10, 10, 580, 50)
        
    def _initFont(self):
        self._font = QtGui.QWidget(self)
        self._font.setGeometry(10, 100, 580, 200)
        self._font.setStyleSheet("QWidget { background-color: %s }"
                                 % QtGui.QColor(200,200,200).name() )

    def _initLabelResultat(self):
        self._labelResultat = QtGui.QLabel("", self )
        self._labelResultat.setGeometry(40, 320, 520, 30)

    def addCueilleur(self, position):
        self._cueilleur = QtGui.QWidget(self)
        self._cueilleur.setGeometry(20, 110 + 50 * position, 35, 35)
        self._cueilleur.setStyleSheet("QWidget { background-color: %s }"
                                      % QtGui.QColor(200,0,0).name() )
    
    def addNourriture(self, nourritures):
        compteur = 0
        for position in nourritures:
            self._nourritures.append(QtGui.QWidget(self))
            self._nourritures[compteur].setGeometry(20 + (compteur +1) * 50,
                                                    110 + 50 * position,
                                                    35, 35 )
            self._nourritures[compteur].setStyleSheet(
                            "QWidget { background-color: %s }"
                             % QtGui.QColor(0,200,0).name() )
            compteur += 1
        
    def _center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2,
                  (screen.height()-size.height())/2 )

class BoutonPause(QtGui.QPushButton):
    def __init__(self, texte, parent, pause, reinitialise):
        super(QtGui.QPushButton, self).__init__(texte, parent)
        self._pause = pause
        self._reinitialise = reinitialise
        self._fin = False
        self._texte = texte

    def mousePressEvent(self, event):
        if not self._fin:
            self._pause()
        else:
            self._reinitialise()
            self._fin = False
            self.setText(self._texte)
        
    def stop(self, nombre):
        self._fin = True
        self.setText("Recommencer")

app = QtGui.QApplication(sys.argv)
ia = IA()
ui = Ui(ia)
ia.addObserver(ui)
ia.start()
ui.show()

sys.exit(app.exec_())
